﻿using System.Collections.Generic;
using System.Xml;

namespace RunCamlSample.QueryBuilders
{
    /// <summary>
    /// Create By: Nguyen Thanh Tung
    /// </summary>
    public class ViewBuilder : BaseQueryBuilder
    {
        readonly XmlDocument viewFieldsDoc = new XmlDocument();

        readonly List<string> fields = new List<string>();

        public ViewBuilder AppendField(string field)
        {
            fields.Add(field);

            return this;
        }

        public ViewBuilder AppendField(string[] fiel)
        {
            fields.AddRange(fiel);

            return this;
        }

        public XmlNode ToXmlNode()
        {
            XmlNode ViewFields = AddXmlElement(viewFieldsDoc, "ViewFields", "");

            foreach (var field in fields)
            {
                AddFieldRef(ViewFields, field); 
            }

            return ViewFields;
        }

        private void AddFieldRef(XmlNode viewFields, string fieldName)
        {
            XmlNode fieldRef = AddXmlElement(viewFields, "FieldRef", "");

            AddXmlAttribute(fieldRef, "Name", fieldName);
        }
    }
}